<?php

class Controller_Admin extends Controller_Admin_Template {
	
	public function action_index() {
		$this->template->title = __('Admin Control Panel');
		
		$this->template->content = View::factory('admin/index');
	}
	
	public function action_register()
	{
		$view = View::factory('admin/register');
		$view->set('values', $_POST);
		$view->bind('errors', $errors);
		
		if($_POST) {
			$user = ORM::factory('user');
			$user->values($_POST);
			
			$extra_rules = ORM::factory('user')
				->get_password_validation($_POST);
			
			try {
				$user->save($extra_rules);
				$user->add('roles', ORM::factory('role')->where('name', '=', 'admin')->find());
				$user->add('roles', ORM::factory('role')->where('name', '=', 'login')->find());
				$this->request->redirect('item/item/viewitems');
			}
			catch (ORM_Validation_Exception $e) {
				$errors = $e->errors('model');	
			}
		}
		$this->template->title = __('Register New Admin');
		$this->template->content = $view;
	}
	
	public function action_inventory() {
		$this->template->title = __('Inventory');
		
		$categories = ORM::factory('category')
			->where('parent_category_id', '=', 0)
			->find_all();
		
		$view = View::factory('admin/inventory/index');
		$view->set('categories', $categories);
		
		$this->template->content = $view;
		
		
	}
	
}